<?php
namespace app\store\controller;

use app\store\model\Archives as ArchivesModel;
use app\store\controller\Controller;
use app\store\model\store\User;

class Archives extends Controller
{
    /**
     * 档案列表
     * @return mixed
     */
    public function index()
    {
        $model = new ArchivesModel;
        $list = $model->getList();
        return $this->fetch('index', compact('list'));
    }
    /**
     * 添加档案
     * @return array|mixed
     */
    public function add()
    {
        $model = new ArchivesModel;
        if (!$this->request->isAjax()) {
            return $this->fetch('add');
        }
        // 新增记录
        if ($model->add($this->postData('archives'))) {
            return $this->renderSuccess('添加成功', url('archives/index'));
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }
    /**
     * 编辑档案
     * @param $category_id
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function edit($id)
    {
        $model = ArchivesModel::detail($id);
        if (!$this->request->isAjax()) {
            return $this->fetch('edit', compact('model'));
        }
        // 更新记录
        if ($model->edit($this->postData('archives'))) {
            return $this->renderSuccess('更新成功', url('archives/index'));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }
    /**
     * 删除档案
     * @param $article_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete($id)
    {
        $model = ArchivesModel::detail($id);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    function subtext($text, $length=8)//省略文本
    {
        if(mb_strlen($text, 'utf8') > $length)
        {
            return mb_substr($text, 0, $length, 'utf8').'…';
        }
        return $text;
    }
}